// pages/product/naodong.js
//获取应用实例  
var app = getApp();
Page({
  data: {
    // types: null,
    currType: 0,
    // 当前类型
    goodsurl: app.d.hostUrl + app.d.goodsurl,
    page: 1,
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var page = that.data.page;
    wx.request({
      url: app.d.hostUrl + 'wapi/category.html',
      method: 'post',
      data: {
        page: page,
        cate_id: that.data.currType,
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        share: 0,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.ret;
        if (status == 0) {
          var list = res.data.data.goods_list;
          if (list.length == 0) {
            wx.showToast({
              title: '没有了',
              duration: 2000
            });
            return false;
          }
          that.setData({
            page: page + 1,
            types: that.data.types.concat(list)
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000,
          });
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })
  },
  onLoad: function (option) {
    var that = this;
    var id = 0;
    console.log(option)
    if (option.id != undefined) {
      id = option.id;
    }
    that.setData({ currType: id });
    that.get_data(id);
  },
  onShow: function () {
    var that = this;
    this.get_data(that.data.currType);
  },
  get_data: function (currType) {
    var that = this;
    wx.request({
      url: app.d.hostUrl + 'wapi/category.html',
      method: 'post',
      data: {
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        share: 0,
        cate_id: currType,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data 
        var status = res.data.ret;
        if (status == 0) {
          var list = res.data.data.goods_list;
          var catList = res.data.data.cate_list;
          that.setData({
            types: list,
            typeTree: catList,
          });
        } else {
          wx.showToast({
            title: res.data.msg,
            duration: 2000,
          });
        }

      },
      error: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000,
        });
      },

    });
  },
  tapType: function (e) {
    var that = this;
    const currType = e.currentTarget.dataset.typeId;
    that.setData({
      currType: currType
    });
    wx.request({
      url: app.d.hostUrl + 'wapi/category.html',
      method: 'post',
      data: {
        cate_id: currType,
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
        share: 0,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var status = res.data.ret;
        if (status == 0) {
          var catList = res.data.data.cate_list;
          var goods_list = res.data.data.goods_list;
          that.setData({
            typeTree: catList,
            types: goods_list,
          });
        } else {
          wx.showToast({
            title: '参数错误',
            duration: 2000,
          });
        }
      },
      error: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 2000,
        });
      }
    });
  },
  // 加载品牌、二级类目数据
  getTypeTree(currType) {
    const me = this, _data = me.data;
    if (!_data.typeTree[currType]) {
      request({
        url: ApiList.goodsTypeTree,
        data: { typeId: +currType },
        success: function (res) {
          _data.typeTree[currType] = res.data.data;
          me.setData({
            typeTree: _data.typeTree
          });
        }
      });
    }
  },
  addShopCart: function (e) { //添加到购物车

    wx.request({
      url: app.d.hostUrl + 'wapi/cart_add.html',
      method: 'post',
      data: {
        uid: app.d.userId,
        pid: e.currentTarget.dataset.productid,
        num: 1,
        seesionid: app.globalData.userInfo.sessionId,
        openid: app.globalData.userInfo.openid,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // //--init data
        var data = res.data;
        if (data.status == 0) {
          var ptype = e.currentTarget.dataset.type;
          if (ptype == 'buynow') {
            wx.redirectTo({
              url: '../order/pay?cartId=' + data.cart_id
            });
            return;
          } else {
            wx.showToast({
              title: '加入购物车成功',
              icon: 'success',
              duration: 2000
            });
          }
        } else {
          wx.showToast({
            title: data.err,
            duration: 2000
          });
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  onShareAppMessage: function () {
    return {
      title: '赚钱那些事儿',
      path: '/pages/index/index?scene=invite%3D' + app.globalData.user_id,
      success: function (res) {
        wx.showToast({
          title: '分享成功',
          duration: 2000
        });
      },
      fail: function (res) {
        // 分享失败
      }
    }
  },
})